const { DataTypes, Model, Deferrable } = require("sequelize");
const sequelize = require("../../sequelize");
const Image = require("./image");
/**
 * 定义图库模型
 */
class ImageClass extends Model {
    //增加图库分类
    static async addImageClass(name,order){
        return await this.create({name:name,order:order})
    }
    //修改图库分类
    static async updateImageClass(id,name,order){
        return await this.update({name:name,order:order},{
            where:{
                id:id
            }
        })
    }
    //删除图库分类
    static async deleteImageClass(id){
        return await this.destroy({
            where:{
                id:id
            }
        })
    }
    static async getImagebyImageClass(limit,page,id){
        let json={}
        await ImageClass.findOne({
            include:[
                {
                    model:Image,
                    limit:limit,
                    offset:limit*page-limit
                }
            ],
            where:{
                id:id
            },
        }).then(async row=>{
            json["list"]=row.Images
            json["totalCount"]=await Image.count({
                where:{
                    image_class_id:id
                }
            })
        })
        return json
    }
};
ImageClass.init({
    id:{
        type:DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name:{
        type:DataTypes.STRING,
    },
    order:{
        type:DataTypes.INTEGER
    }
}, {
    sequelize,
    timestamps:false,
    modelName: "ImageClass",
    tableName: "image_class",
})
module.exports = ImageClass